package com.cn.lf.wx;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.cn.lf.wx.dacs.bussiness.author.action.Base;
import com.cn.lf.wx.dacs.bussiness.author.utils.Constant;
import com.cn.lf.wx.dacs.bussiness.author.utils.JwtUtils;
import com.cn.lf.wx.dacs.bussiness.author.utils.StringUtil;
import com.cn.ym.log.Log;


@Configuration
public class Authconfig extends Base implements HandlerInterceptor {
	
  
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
    	//Map<String,Object> header=getHeaderMap(request);
    	String token = request.getHeader(Constant.Setting.HEADER_AUTH);
//    	
    	//String requString=request.getPathInfo();
    	//String url =request.getRequestURI();
    	if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(HttpServletResponse.SC_OK);
           
            return true;
        }
    	Log.info("auth login token ... === {}"+token);
    	if(StringUtil.isEmpty(token)) {
    		return false;
    	}
    	Log.info("auth login token === {}"+token);
    	
        try {
            JwtUtils.parseJWT(token);

            Log.info("auth login token ok....");
        	return true;
        } catch (Exception e) {
        	Log.error("token 解析 {}"+ e.getMessage());
            return false;
        }
//    	return true;
    	
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
 
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
 
    }
   /* //返回错误信息
    private static void setReturn(HttpServletResponse response, int status, String msg) throws IOException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpResponse.setHeader("Access-Control-Allow-Origin", HttpContextUtil.getOrigin());
        //UTF-8编码
        httpResponse.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        Result build = Result.build(status, msg);
        String json = JSON.toJSONString(build);
        httpResponse.getWriter().print(json);
    }*/
 
}